<template>
  <div>
    <div>
      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="officeForm" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <!--<a-col :span="12">
                <a-form-model-item label="劳动者名称" :labelCol="labelCol"  :wrapperCol="wrapperCol" prop="workerId">
                  &lt;!&ndash;<el-button  type="info" @click="viewWorkList()">查询劳动者</el-button>&ndash;&gt;
                  &lt;!&ndash;<a-button @click="showViewWorkList()" style="margin-bottom: 0;">查询劳动者</a-button>&ndash;&gt;
                  <a-input v-model="model.workerName" @click="showViewWorkList()" placeholder="点击选择劳动者"  >
                  </a-input>
                  <a-input v-model="model.workerId" placeholder="劳动者ID"  type="hidden">
                  </a-input>

                </a-form-model-item>
              </a-col>-->
             <!-- <a-col :span="12">
                <a-form-model-item label="关系表ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relationId">
                  <a-input v-model="model.relationId" placeholder="请输入关系表ID"  ></a-input>
                </a-form-model-item>
              </a-col>-->
              <!--<a-col :span="12">
                <a-form-model-item label="入职站点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onboardingSiteId">
                  <a-input v-model="model.onboardingSiteId" placeholder="请输入入职站点"  ></a-input>
                </a-form-model-item>
              </a-col>-->
              <a-col :span="12">
                <a-form-model-item label="入职站点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onboardingSite">
                  <a-input v-model="model.onboardingSiteId" placeholder="请输入入职站点" type="hidden"></a-input>
                  <j-search-select-tag v-model="model.onboardingSite" :dictOptions="dictOptions" placeholder="请选择入职站点" @change="siteChange"/>
                </a-form-model-item>
              </a-col>
              <!--<a-col :span="12">
                <a-form-model-item label="入职主体公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onCompanyId">
                  <a-input v-model="model.onCompanyId" placeholder="请输入入职主体公司"  ></a-input>
                </a-form-model-item>
              </a-col>-->
              <a-col :span="12">
                <a-form-model-item label="入职主体公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onCompany">
                  <a-input v-model="model.onCompanyId" placeholder="请输入入职主体公司" type="hidden" ></a-input>
                  <a-input v-model="model.onCompany" placeholder="请输入入职主体公司名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="入职所在城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityCode">
                  <a-input v-model="model.cityCode" placeholder="请输入入职所在城市"  type="hidden" ></a-input>
                  <a-input v-model="model.cityName" placeholder="请输入入职所在城市"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employerFeedback">
                  <j-dict-select-tag type="list" v-model="model.employerFeedback" dictCode="employer_feedback	" placeholder="请选择状态" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="用工反馈上岗时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workTime">
                  <j-date placeholder="请选择用工反馈上岗时间" v-model="model.workTime"  style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="用工反馈离职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveTime">
                  <j-date placeholder="请选择用工反馈离职时间" v-model="model.leaveTime"  style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="确认入职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="confirmOnboardingTime">
                  <j-date placeholder="请选择确认入职时间" v-model="model.confirmOnboardingTime"  style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="复工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkTime">
                  <j-date placeholder="请选择复工时间" v-model="model.reworkTime"  style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>

    </div>
<workList ref="workList" v-on:workerChanged="setWorkerInfo"></workList>
  </div>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import workList from '@/views/crm/component/workList'

  export default {
    name: 'OfficeForm',
    components: {
      workList
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      row:["row"]
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        dictOptions:[],
        url: {
          add: "/office/empOffice/add",
          edit: "/office/empOffice/edit",
          queryById: "/office/empOffice/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDictConfig();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      showViewWorkList() {
        let _this = this;
        this.$refs.workList.showViewWorkList(_this);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      setWorkerInfo(row){   //声明这个函数
        this.$set(this.model,"workerName",row.name);
        this.$set(this.model,"workerId",row.id);
        this.$set(this.model,"relationId",row.relationId);
      },
      //加载数据字典
      initDictConfig(){
        var parameter = {};
        parameter.deptIds = 14852;
        getAction("/office/empOffice/queryAdminDeptSite",parameter).then((res)=>{
          if (res.success == true) {
            let dictsData = res.result;
            if (dictsData && dictsData.length > 0) {
              for (let i = 0; i < dictsData.length; i++) {
                let item = {};
                item.value = dictsData[i].deptId;
                item.text = dictsData[i].name;
                this.dictOptions.push(item);
                ////console.log(dictoption)
              }
            }
            console.log(this.dictOptions)
          }
        });
      },
      siteChange(e){
        console.log(e);

        this.$set(this.model,"onboardingSiteId",e);
        var name;
        for (let i = 0; i < this.dictOptions.length; i++) {
          let item = this.dictOptions[i];
          if(item.value == e){
            name = item.text;
            break;
          }
        }
        this.$set(this.model,"onboardingSite",name);

        //查询站点上级
        var parameter = {};
        parameter.deptId = e;
        getAction("/office/empOffice/querySiteSuper",parameter).then((res)=>{
          if (res.success == true) {
            let dictsData = res.result;
            console.log(this.dictsData)
            this.$set(this.model,"onCompanyId",dictsData.subjectId);
            this.$set(this.model,"onCompany",dictsData.subjectName);
            this.$set(this.model,"cityCode",dictsData.cityCode);
            this.$set(this.model,"cityName",dictsData.cityName);
          }
        });
      }
    }
  }
</script>